﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Tanks
{
    class ChooseMenu
    {
        // Прямоугольник изображения
        //protected Rectangle imageRect;
        KeyboardState keyboard; 

        
        private enum players
        {
            one,
            two
        }

        bool isLeftUp = true;
        bool isRighUp = true;
        bool isEnterUp = true;

        Game1 game;
        static IFieldObject background;
        private Tanks activTank;
        players activPlayer;

        public ChooseMenu(Game1 game)
        {
            this.game = game;
            activTank = Tanks.mouse;
        }

        public void update()
        {
            keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Left) && isLeftUp)
            {
                isLeftUp = false;
                if(activTank > Tanks.mouse)
                    activTank -= 1;
            }
            else if (keyboard.IsKeyDown(Keys.Right) && isRighUp)
            {
                isRighUp = false;
                if (activTank < Tanks.e50)
                    activTank += 1;
            }

            if (keyboard.IsKeyUp(Keys.Left))
                isLeftUp = true;
            if (keyboard.IsKeyUp(Keys.Right))
                isRighUp = true;

            if (keyboard.IsKeyDown(Keys.Enter) && isEnterUp)
            {
                isEnterUp = false;
                if (activPlayer == players.one)
                {
                    game.playerOneTank = activTank;
                    game.addTank(game.playerOneTank, 1);
                    activPlayer = players.two;
                    activTank = Tanks.mouse;
                }
                else
                {
                    game.playerTwoTank = activTank;
                    game.addTank(game.playerTwoTank, 2);
                    game.gameStatus = gamest.game;
                }
            }
            if (keyboard.IsKeyUp(Keys.Enter))
                isEnterUp = true;
        }

        public void draw()
        {
            background = new Background(Resources.Background, game);

            game.SpriteBatch.Begin();
            background.Draw();
            
            switch(activPlayer)
            {
                case players.one:
                    game.SpriteBatch.Draw(Resources.ch1, new Rectangle(200, 200, Resources.ch1.Width, Resources.ch1.Height), Color.Yellow);
                    break;
                case players.two:
                    game.SpriteBatch.Draw(Resources.ch2, new Rectangle(200, 200, Resources.ch1.Width, Resources.ch1.Height), Color.Yellow);
                    break;
            }
            drawRectangle();
            game.SpriteBatch.Draw(Resources.Mouse, new Rectangle(250, 250, Resources.Mouse.Width, Resources.Mouse.Height), Color.White);
            game.SpriteBatch.Draw(Resources.t34, new Rectangle(250 + Resources.Mouse.Width + 10, 250, Resources.t34.Width, Resources.t34.Height), Color.White);
            game.SpriteBatch.Draw(Resources.e50,
                new Rectangle(250 + Resources.Mouse.Width + 10 + Resources.t34.Width + 10, 250, Resources.e50.Width, Resources.e50.Height), 
                Color.White);

            game.SpriteBatch.End();
        }

        private void drawRectangle()
        {
            Texture2D texture = new Texture2D(game.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            Int32[] pixel = { 0xFFFFFF };
            texture.SetData<Int32>(pixel, 0, texture.Width * texture.Height);

            switch (activTank)
            {
                case Tanks.mouse:
                    game.SpriteBatch.Draw(texture, new Rectangle(250 - 5, 250 - 5, Resources.Mouse.Width + 10, Resources.Mouse.Height + 10), Color.Red);
                    break;
                case Tanks.t34:
                    game.SpriteBatch.Draw(texture, new Rectangle(250 + Resources.Mouse.Width + 10 - 5,
                        250 - 5, Resources.t34.Width + 10, Resources.t34.Height + 10), Color.Red);
                    break;
                case Tanks.e50:
                    game.SpriteBatch.Draw(texture, new Rectangle(250 + Resources.Mouse.Width + 10 + Resources.t34.Width + 10 - 5, 250 - 5, Resources.e50.Width + 10, Resources.e50.Height + 10), Color.Red);
                    break;
            }
            
        }
    }
}
